package com.lxl.bt.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lxl.bt.R;
import com.lxl.bt.bean.AgentIncomeBean;
import com.lxl.bt.bean.AgentIncomeContentBean;
import com.lxl.bt.net.NetApi;
import com.lxl.bt.net.Result;
import com.lxl.bt.retorfit.NetCallback;
import com.lxl.bt.retorfit.RetrofitUtil;
import com.lxl.bt.util.ToastUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.yanzhenjie.sofia.Sofia;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AgentIncomeActivity extends BaseActivity {

    @BindView(R.id.rlBack)
    RelativeLayout rlBack;
    @BindView(R.id.txt_toolbar_title)
    TextView txtToolbarTitle;

    @BindView(R.id.tv_amount)
    TextView tvAmount;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private int pageNo = 0;
    private int pageSize = 20;
    private List<String> sort = new ArrayList<>();
    private AgentIncomeAdapter agentAdapter;

    public static void intent(Context fromContext,int userId){
        Intent intent = new Intent(fromContext, AgentIncomeActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("userId",userId);
        intent.putExtras(bundle);
        fromContext.startActivity(intent);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_agent_income);

        ButterKnife.bind(this);

        Sofia.with(this).invasionStatusBar()
                .statusBarLightFont()
                .statusBarBackgroundAlpha(0)
                .invasionNavigationBar()
                .navigationBarBackgroundAlpha(0);

        rlBack.setVisibility(View.VISIBLE);
        txtToolbarTitle.setText(getResources().getString(R.string.wk_agent_income));

        Bundle bundle = getIntent().getExtras();
        int userId = bundle.getInt("userId");
        initRecyclerView(userId);
    }

    private void initRecyclerView(int userId) {
        sort.add("id,desc");
        refreshLayout.setPrimaryColors(0,0,0);
        //下拉刷新
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            pageNo = 0;
            agentIncomes(userId);
        });
        refreshLayout.setEnableLoadMore(true);
        //上拉加载
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                agentIncomes(userId);
            }
        });

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(agentAdapter = new AgentIncomeAdapter(null));

        //进入就刷新数据
        refreshLayout.autoRefresh();
    }

    private void setEmptyView() {
        View emptyView = View.inflate(this, R.layout.empty_view, null);
        agentAdapter.setEmptyView(emptyView);
    }

    private void agentIncomes(int userId){
        RetrofitUtil.getInstance().create(NetApi.class, 0)
                .agentIncomes(pageNo,pageSize,6790,sort).enqueue(new NetCallback<Result<AgentIncomeBean>>() {
            @Override
            public void onSuccess(Result<AgentIncomeBean> result) {
                if (result.isSuccess()) {
                    AgentIncomeBean data = result.getData();
                    if (pageNo == 0){
                        //刷新
                        agentAdapter.setNewData(data.content);
                        refreshLayout.finishRefresh();
                        refreshLayout.setEnableLoadMore(true);
                        if (data.content != null && data.content.size() > 0){
                            tvAmount.setText(data.content.get(0).totalIncomes);
                        }
                        if (data.content == null || data.content.size() == 0){
                            setEmptyView();
                        }
                    }else{
                        //加载更多
                        refreshLayout.finishLoadMore();
                        if (data != null){
                            agentAdapter.addData(data.content);
                        }
                    }
                    if (data == null || data.content.size() < pageSize) {
                        //没有更多了
                        refreshLayout.setEnableLoadMore(false);
                    }else{
                        refreshLayout.finishLoadMore();
                    }
                    pageNo++;
                }else{
                    ToastUtil.showToast(AgentIncomeActivity.this,result.getMsg());
                }
            }

            @Override
            public void netLogin() {

            }

            @Override
            public void onError(String errorStr) {
                super.onError(errorStr);
                Log.w("lxl", errorStr);
            }
        });
    }

    static class AgentIncomeAdapter extends BaseQuickAdapter<AgentIncomeContentBean, BaseViewHolder>{

        public AgentIncomeAdapter(@Nullable List<AgentIncomeContentBean> data) {
            super(R.layout.item_agent_income, data);
        }

        @Override
        protected void convert(@NonNull BaseViewHolder baseViewHolder, AgentIncomeContentBean agentIncomeBean) {
            baseViewHolder.setText(R.id.tv_income_type,agentIncomeBean.reasonStr);
            baseViewHolder.setText(R.id.tv_date,agentIncomeBean.createDate);
            baseViewHolder.setText(R.id.tv_amount,agentIncomeBean.amount);
        }
    }

    @OnClick({R.id.rlBack})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rlBack:
                finish();
                break;
        }
    }
}